﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FXnaGis;



using FXnaAppBase;

using NSCore;




namespace NSApplication
{
    public class GisService
    {

        #region 事件

        public event EventHandler MapModeChanged;

        #endregion



        protected GisService()
        {

        }

        static GisService currentService;

        public static GisService CurrentService
        {
            get
            {

                if (currentService == null)
                {
                    currentService = new GisService();

                }
                return GisService.currentService;
            }

        }





        FMap _currentMap;

        public FMap CurrentMap
        {
            get { return _currentMap; }
            set
            {

                _currentMap = value;

                this.OnSetMap();


            }
        }

        GisProjectListener _projectListener;

        GisMapFunctionListener _mapfunctionListener;




        //mapmode的临时存储问题

        MapMode _mapMode = MapMode.None;

        public MapMode MapMode
        {
            get { return _mapMode; }
            set {

                if (this._mapMode!=value)
                {

                    _mapMode = value;

                    this.SetMapMode(this._mapMode);


                    //激活对应的事件

                    if (MapModeChanged!=null)
                    {
                        MapModeChanged(this,EventArgs.Empty);
                        
                    }

                    
                }
               



            
            }
        }




        protected void OnSetMap()
        {

            //初始化控件控制模式等

            if (this._currentMap != null)
            {





                //MF监听器，初始化，并开始监听，因为这里假设监听内容不变


                this._mapfunctionListener = new GisMapFunctionListener(this._currentMap);




                //project 监听器初始化 ，但还未开始监听

                this._projectListener = new GisProjectListener(this._currentMap);



                //这里我们测试一个有趣的东西，
                this._currentMap.GeoMapFrame.LayerService.SelectionChanged += new EventHandler(LayerService_SelectionChanged);




            }



        }


        void LayerService_SelectionChanged(object sender, EventArgs e)
        {
            List<FShape> list = this._currentMap.GeoMapFrame.LayerService.GetSelectedShapes();



          

        }




        public void Init()
        {
            ProjectService.CurrentService.ProjChanged += new FXnaInfrastructure.ProjHandler(CurrentService_ProjChanged);

        }



        void CurrentService_ProjChanged(FXnaInfrastructure.ProjChangedType chType)
        {
            switch (chType)
            {
                case FXnaInfrastructure.ProjChangedType.NEW:
                    this.CreateScene();

                    break;
                case FXnaInfrastructure.ProjChangedType.SAVE:
                    break;
                case FXnaInfrastructure.ProjChangedType.CLOSE:
                    this.ClearScene();
                    break;
                case FXnaInfrastructure.ProjChangedType.OPEN:
                    this.CreateScene();

                    break;

                default:
                    break;
            }
        }



        protected void ClearScene()
        {
            this._currentMap.GeoMapFrame.LayerService.Clear();

        }






        /// <summary>
        /// 基础数据读入
        /// </summary>
        /// <param name="project"></param>
        protected void ReadProject(Project project)
        {


            //公用变量减少代码层次

            LayerService layerService = this._currentMap.GeoMapFrame.LayerService;


            //对于未明确定义ID的，我们使用集合的类名作为其名称，之后可能会有问题，我们需要之后进行再设计

            FGroupLayer imageGroup = new FGroupLayer(NSCoreNames.ImageLayers);

            this._currentMap.GeoMapFrame.LayerService.Add(imageGroup);



            //通用数据写入




            string imagePath = ProjectService.CurrentService.GetProjPath(ProjPath.Images);


            foreach (Bitmap item in project.CommonData.Images.Images)
            {
                FBitmapLayer blayer = new FBitmapLayer(item.FileNameWithoutExt, imagePath + item.FileName, item.Extent);

                imageGroup.Add(blayer);

            }







            //构建矢量图层

            foreach (FGroupLayer group in project.CommonData.VectorLayers.Entities)
            {

                this._currentMap.GeoMapFrame.LayerService.Add(group);


            }




            //构建等值线图层,在这里做一个实验


            FVectorLayer contourLayer = project.NSResult.ContourLayer;
            this._currentMap.GeoMapFrame.LayerService.Add(contourLayer);






            //构建网格图层

            FVectorLayer gridLayer = project.NSData.GridLayer;

            this._currentMap.GeoMapFrame.LayerService.Add(gridLayer);


            //构建模型图层





            FVectorLayer ptLayer = project.NSData.PtLayer;

            this._currentMap.GeoMapFrame.LayerService.Add(ptLayer);




            //FVectorLayer lnLayer = project.ASData.LnLayer;
            //this._currentMap.GeoMapFrame.LayerService.Add(lnLayer);



            //FVectorLayer polyLayer = project.ASData.PolyLayer;
            //this._currentMap.GeoMapFrame.LayerService.Add(polyLayer);





            //障碍物图层

            FVectorLayer barrierLayer = project.NSData.BarrierLayer;

            this._currentMap.GeoMapFrame.LayerService.Add(barrierLayer);






            //FVectorLayer cubeLayer = project.ASData.CubeLayer;
            //this._currentMap.GeoMapFrame.LayerService.Add(cubeLayer);


            //FVectorLayer rectLayer = project.ASData.RectLayer;
            //this._currentMap.GeoMapFrame.LayerService.Add(rectLayer);


           





            //FVectorLayer circleLayer = project.ASData.CircleLayer;
            //this._currentMap.GeoMapFrame.LayerService.Add(circleLayer);





            //FVectorLayer expLayer = project.ASData.ExpLayer;
            //this._currentMap.GeoMapFrame.LayerService.Add(expLayer);




            ////定义其他图层,网格图层

            //FVectorLayer receptorLayer = project.ASData.ReceptorLayer;
            //this._currentMap.GeoMapFrame.LayerService.Add(receptorLayer);



            //FVectorLayer gridLayer = project.ASData.GridLayer;

            //this._currentMap.GeoMapFrame.LayerService.Add(gridLayer);



            //FVectorLayer buildLayer = project.ASData.Buildings;

            //this._currentMap.GeoMapFrame.LayerService.Add(buildLayer);







        }



        protected void CreateScene()
        {


            if (ProjectService.CurrentService.BNullProj)
            {
                return;

            }


            Project project = ProjectService.CurrentService.Project;




            //project.ASData.PtSources.


            this._currentMap.GeoMapFrame.SuspendEvents();



            //主场景创建函数



            //读入项目图层数据
            this.ReadProject(project);


            this._currentMap.GeoMapFrame.ResumeEvents();




            //开启主程序事件的监听

            this._projectListener.ListenProject(project);






        }




        protected void SetMapMode(MapMode mode)
        {

            switch (mode)
            {
                case MapMode.Select:
                    this._currentMap.FunctionService.SetCurrentMapFunction(typeof(FSelectMF));
                    break;

                case MapMode.Move:

                    this._currentMap.FunctionService.SetCurrentMapFunction(typeof(FMoveMF));

                    break;


                case MapMode.Pan:

                    this._currentMap.FunctionService.SetCurrentMapFunction(typeof(FPanMF));

                    break;
                case MapMode.AddPoint:


                    this._mapfunctionListener.SimpleGeometryMF.Mode = FSimpleGemotryEditMode.POINT;

                    this._currentMap.FunctionService.SetCurrentMapFunction(typeof(FSimpleGeometryEditMF));

                    //设置当前图层
                    this._currentMap.GeoMapFrame.LayerService.SelectedLayer = this._currentMap.GeoMapFrame.LayerService.GetLayer(NSCoreNames.PointSourceLayer);




                    break;
                case MapMode.AddLine:

                    this._mapfunctionListener.SimpleGeometryMF.Mode = FSimpleGemotryEditMode.LINE;

                    this._currentMap.FunctionService.SetCurrentMapFunction(typeof(FSimpleGeometryEditMF));

                    //设置当前图层
                    this._currentMap.GeoMapFrame.LayerService.SelectedLayer = this._currentMap.GeoMapFrame.LayerService.GetLayer(NSCoreNames.LineSourceLayer);




                    break;
                case MapMode.AddPoly:

                    this._mapfunctionListener.SimpleGeometryMF.Mode = FSimpleGemotryEditMode.POLY;

                    this._currentMap.FunctionService.SetCurrentMapFunction(typeof(FSimpleGeometryEditMF));

                    //设置当前图层
                    this._currentMap.GeoMapFrame.LayerService.SelectedLayer = this._currentMap.GeoMapFrame.LayerService.GetLayer(NSCoreNames.PolySourceLayer);




                    break;




                case MapMode.AddBarrier:


                      this._mapfunctionListener.SimpleGeometryMF.Mode = FSimpleGemotryEditMode.LINE;

                    this._currentMap.FunctionService.SetCurrentMapFunction(typeof(FSimpleGeometryEditMF));

                    //设置当前图层
                    this._currentMap.GeoMapFrame.LayerService.SelectedLayer = this._currentMap.GeoMapFrame.LayerService.GetLayer(NSCoreNames.BarrierLayer);


                    break;


                    break;



                case MapMode.AddRect:


                    this._currentMap.FunctionService.SetCurrentMapFunction(typeof(FRectMF));

                    //设置当前图层
                    this._currentMap.GeoMapFrame.LayerService.SelectedLayer = this._currentMap.GeoMapFrame.LayerService.GetLayer(NSCoreNames.RectSourceLayer);


                    break;


                case MapMode.AddCircle:


                    this._currentMap.FunctionService.SetCurrentMapFunction(typeof(FCircleMF));

                    //设置当前图层
                    this._currentMap.GeoMapFrame.LayerService.SelectedLayer = this._currentMap.GeoMapFrame.LayerService.GetLayer(NSCoreNames.CircleSourceLayer);


                    break;


                case MapMode.AddCube:

                    this._currentMap.FunctionService.SetCurrentMapFunction(typeof(FCubeMF));

                    //设置当前图层
                    this._currentMap.GeoMapFrame.LayerService.SelectedLayer = this._currentMap.GeoMapFrame.LayerService.GetLayer(NSCoreNames.CubeSourceLayer);


                    break;

                




                default:
                    break;
            }

        }




    }
}
